package com.dianning.service.imp;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import org.apache.commons.logging.*;

import com.dianning.bean.AdSpace;
import com.dianning.bean.Message;
import com.dianning.bean.Visitor;
import com.dianning.dao.AdAdsDAO;
import com.dianning.dao.AdSpaceDAO;
import com.dianning.dao.MessageDAO;
import com.dianning.dao.UserInfoDAO;
import com.dianning.dao.VisitorDAO;
import com.dianning.exception.DianningException;
import com.dianning.service.AdService;
import com.dianning.service.MessageService;
import com.dianning.service.VisitorService;
import com.dianning.service.web.PageList;
import com.dianning.service.web.Pages;

public class MessageServiceImp implements MessageService {

	private static final Log logger = LogFactory.getLog(MessageServiceImp.class);

	private MessageDAO messageDAO;
	private UserInfoDAO userInfoDAO;


	public MessageDAO getMessageDAO() {
		return messageDAO;
	}

	public void setMessageDAO(MessageDAO messageDAO) {
		this.messageDAO = messageDAO;
	}
	

	public UserInfoDAO getUserInfoDAO() {
		return userInfoDAO;
	}

	public void setUserInfoDAO(UserInfoDAO userInfoDAO) {
		this.userInfoDAO = userInfoDAO;
	}

	/**
	 * 保存Message对象
	 * 
	 * @param message
	 * 			Message对象
	 * @return Message
	 * 			Message对象
	 */
	public Message[] createMessage(Message fromuMessage,Message toMessage){
		fromuMessage = this.getMessageDAO().saveMessage(fromuMessage);
		toMessage =  this.getMessageDAO().saveMessage(toMessage);
		Message[] message = {fromuMessage,toMessage};
		return message;
	}
	
	/**
	 * 保存Message对象
	 * 
	 * @param message
	 * 			Message对象
	 * @return Message
	 * 			Message对象
	 */
	public Message updateMessage(Message message){
		return this.getMessageDAO().updateMessage(message);
	}
	
	public void updateInboxMessageIsnew(long touid,int isnew){
		this.getMessageDAO().updateInboxMessageIsnew(touid, isnew);
	}
	
	/**
	 * 根据主键ID获取Message对象
	 * 
	 * @param id
	 * 			主键ID
	 * @return Message
	 * 			Message对象
	 */
	public Message findMessageById(long id){
		Message m = this.getMessageDAO().findMessageById(id);
		m.setFromUserInfo(this.getUserInfoDAO().findUserInfoById(m.getFromuid()));
		m.setToUserInfo(this.getUserInfoDAO().findUserInfoById(m.getTouid()));
		return m;
	}
	
	/**
	 * 获取Message对象列表
	 * @param fromuid
	 * @param touid
	 * @param folder
	 * @param isnew
	 * @param delstatus
	 * @param orderby
	 * @param ascOrDesc
	 * @param pages
	 * @return PageList
	 */
	public PageList findMessages(long fromuid,long touid,String folder,int isnew,int delstatus,String orderby, int ascOrDesc,Pages pages){
		PageList pl = new PageList();
        if (pages.getTotalNum() == -1) {
            pages.setTotalNum(this.getMessageDAO().getMessageNum(fromuid, touid, folder, isnew, delstatus));
        }
        pages.executeCount();
        if(pages.getTotalNum() == 0){
        	pl.setObjectList(null);
        	pl.setPages(pages);
        	return pl;
        }
		List l = this.getMessageDAO().findMessages(fromuid, touid, folder, isnew, delstatus, orderby, ascOrDesc,pages.getSpage(), pages.getPerPageNum());
		for(int i=0;i<l.size();i++){
			Message m = (Message) l.get(i);
			m.setFromUserInfo(this.getUserInfoDAO().findUserInfoById(m.getFromuid()));
			m.setToUserInfo(this.getUserInfoDAO().findUserInfoById(m.getTouid()));
		}
		pl.setObjectList(l);
	    pl.setPages(pages);
	    return pl;
	}
}
